package com.ruoyi.subject;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.ruoyi.exam.domain.Category;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class SubjectManager {
    @Value("${ruoyi.subjectPath}")
    private String subjectPath;

    private List<Category> categories;

    public static void main(String[] args) throws Exception{
        SubjectManager subjectManager =  new SubjectManager();
        subjectManager.init();
        List<Category> list = subjectManager.getCategories();
        Map<Integer , Map<String ,Object>> map = new HashMap<>();
        for(Category category : list){
            Map<String ,Object> tmp = category.toMap();
            map.put(category.getCategory() ,tmp);
        }
        System.out.println(map);
    }

    @PostConstruct
    public void init() throws Exception{
        InputStream in;
        if(StrUtil.isBlank(subjectPath)){
            subjectPath = "subject.json";
            in = readByClassPath(subjectPath);
        }else if(subjectPath.startsWith("http://") || subjectPath.startsWith("https://")){
            in = readByUrl(subjectPath);
        }else {
            in = readByFile(subjectPath);
        }
        if(in != null){
            log.info("find subject config ["+ subjectPath +"]");

            String str = IoUtil.read(in , Charset.forName("utf-8"));
            categories = JSON.parseArray(str , Category.class);
            for(Category c : categories){
                c.getSubjects().stream().forEach(sub -> sub.setCategoryId(c.getCategory()));
            }
        }else {
            throw new FileNotFoundException("redis.yaml");
        }
    }

    /**
     * 获取类别 -> 科目 配置
     * @return
     */
    public List<Category> getCategories(){
        return categories;
    }

    /**
     * 配置更新
     * @throws Exception
     */
    public void onConfigUpdate() throws Exception{
        init();
    }

    private InputStream readByUrl(String url) throws Exception{
        return HttpRequest.get(url)
                .timeout(3000)
                .execute()
                .bodyStream();
    }

    private InputStream readByFile(String path) throws Exception{
        return FileUtil.getInputStream(path);
    }

    private InputStream readByClassPath(String path) throws Exception{
        ClassPathResource classPathResource = new ClassPathResource(path, this.getClass().getClassLoader());
        return classPathResource.getInputStream();
    }
}
